package com.jsnider.alarmexample;


import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
       final static private long ONE_SECOND = 1000;
       final static private long TWENTY_SECONDS = ONE_SECOND * 5;
       PendingIntent pi;
       BroadcastReceiver br;
       AlarmManager am;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              setup();
              findViewById(R.id.the_button).setOnClickListener(this);
       }
       
       private void setup() {
    	      br = new BroadcastReceiver() {
    	             @Override
    	             public void onReceive(Context c, Intent i) {
    	                    Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
    	                    }
    	             };
    	      registerReceiver(br, new IntentFilter("com.jsnider.alarmexample") );
    	      pi = PendingIntent.getBroadcast( this, 0, new Intent("com.jsnider.alarmexample"),
    	0 );
    	      am = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
    	}
       
       @Override
       public void onClick(View v) {
              am.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 
       TWENTY_SECONDS, pi );
       }
       
       @Override
       protected void onDestroy() {
              am.cancel(pi);
              unregisterReceiver(br);
              super.onDestroy();
       }
}






//import java.util.Calendar;
//
//import android.os.Bundle;
//import android.os.SystemClock;
//import android.app.Activity;
//import android.app.AlarmManager;
//import android.app.PendingIntent;
//import android.content.BroadcastReceiver;
//import android.content.Context;
//import android.content.Intent;
//import android.content.IntentFilter;
//import android.view.Menu;
//import android.view.View;
//import android.widget.Toast;
//
//public class MainActivity extends Activity {
//
////	long ONE_SECOND = 1000;
////	long TWENTY_SECONDS = ONE_SECOND * 5;
////    PendingIntent pendingIntent;
////    Intent intent;
////    BroadcastReceiver broadcastReceiver;
////    AlarmManager alarmManager;
////    IntentFilter filter;
//	
//	@Override
//	protected void onCreate(Bundle savedInstanceState) {
//		
//		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_main);
//		
////		AlarmManager alarmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
////		 Intent intent = new Intent(this, ShortTimeEntryReceiver.class);   
////		 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,  intent, PendingIntent.FLAG_UPDATE_CURRENT);
////		 alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),1000, pendingIntent); 
//		
//		 Calendar cal = Calendar.getInstance();
//
//		 Intent intent = new Intent(this, MainActivity.class);
//		 PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
//
//		 AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//		 // Start every 30 seconds
//		 alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 3*1000, pintent);  
//		 
//		 
//		
//	}
////	
////	public class ShortTimeEntryReceiver extends BroadcastReceiver{
////		@Override
////		public void onReceive(Context context, Intent intent) {
////
////		 try {
////			 	Toast.makeText(getBaseContext(), "First test", Toast.LENGTH_LONG).show();
////		         Bundle bundle = intent.getExtras();
////		         String message = bundle.getString("alarm_message");
////		         // Your activity name
////		         Intent newIntent = new Intent(context, MainActivity.class); 
////		         newIntent.putExtra("alarm_message", message);
////		         newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
////		         context.startActivity(newIntent);
////		        } catch (Exception e) { 
////		         e.printStackTrace();
////
////		        } 
////		} 
////		}
////	
////	
////	public class OnAlarmReceive extends BroadcastReceiver {
////		 
////		  @Override
////		  public void onReceive(Context context, Intent intent) {
////		 
////			  Toast.makeText(getBaseContext(), "First test", Toast.LENGTH_LONG).show();
////			  MainActivity.this.finish();
////		     // Start the MainActivity
//////		     Intent i = new Intent(context, MainActivity.class);
//////		     
//////		     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//////		     context.startActivity(i);
////			  
////		  }
////	}
////	
////	private void setupAlarm(int seconds) {
////		  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
////		  Intent intent = new Intent(getBaseContext(), OnAlarmReceive.class);
////		  PendingIntent pendingIntent = 
////				  PendingIntent.getBroadcast(
////						  MainActivity.this, 0, intent,
////						  PendingIntent.FLAG_UPDATE_CURRENT);
////		 
////		  // Getting current time and add the seconds in it
////		  Calendar cal = Calendar.getInstance();
////		  cal.add(Calendar.SECOND, seconds);
////		 
////		  alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
////		 
////		  // Finish the currently running activity
////		  //MainActivity.this.finish();
////		}
////		
////		
////		
////		
////		broadcastReceiver = new BroadcastReceiver() {
////			@Override
////			public void onReceive(Context c, Intent i) {
////				Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
////				}
////            };
////        registerReceiver(broadcastReceiver, new IntentFilter("com.jsnider.alarmexample") );     
////        alarmManager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
////        intent = new Intent();
////        filter = new IntentFilter();
////        PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
////        alarmManager.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + TWENTY_SECONDS, pendingIntent );
////        registerReceiver (broadcastReceiver, filter);
////	}
////	
////	public void setAlarm(View v) {
////		
////		alarmManager.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + TWENTY_SECONDS, pendingIntent );
////		Toast.makeText(getBaseContext(), "First test", Toast.LENGTH_LONG).show();
////	}
////	
////	@Override
////	protected void onDestroy() {
////		
////		alarmManager.cancel(pendingIntent);
////	    unregisterReceiver(broadcastReceiver);
////	    super.onDestroy();
////	}
//
//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.main, menu);
//		
//		return true;
//	}
//
//}
